package com.hyperz.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.io.SVNRepository;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.hyperz.svn.SVNConnect;
import com.hyperz.svn.SVNConnect.ProxyDetails;
import com.hyperz.svn.SVNConnect.RepositoryDetails;

public class Repository {

	private static final String LOG = "Repository";
	private RepositoryDetails mRepoDetails = null;
	private ProxyDetails mProxyDetails = null;
	private ExecutorService mExecutor = null;
	private SVNRepository mRepository = null;
	private long mLatestRevision = 0;
	private Context mContext = null;
	
	public Repository(Context ctx, Bundle details) {
		
		mContext = ctx;
		loadRepoDetailsFromBundle(details);
		loadProxyDetailsFromBundle(details);
		
	}
	
	private void loadRepoDetailsFromBundle(Bundle bundle) {
		mRepoDetails = new RepositoryDetails();
		
		if (bundle.containsKey(mContext.getPackageName() + ".repo_url"))
			mRepoDetails.setRepositoryURL(bundle.getString(mContext.getPackageName() + ".repo_url"));
		if (bundle.containsKey(mContext.getPackageName() + ".repo_path"))
			mRepoDetails.setRepositoryPath(bundle.getString(mContext.getPackageName() + ".repo_path"));
		if (bundle.containsKey(mContext.getPackageName() + ".repo_uid"))
			mRepoDetails.setUserID(bundle.getString(mContext.getPackageName() + ".repo_uid"));
		if (bundle.containsKey(mContext.getPackageName() + ".repo_pwd"))
			mRepoDetails.setPassword(bundle.getString(mContext.getPackageName() + ".repo_pwd"));
		
	}
	
	private void loadProxyDetailsFromBundle(Bundle bundle) {
		mProxyDetails = new ProxyDetails();
		
		if (bundle.containsKey(mContext.getPackageName() + ".proxy_host"))
			mProxyDetails.setProxyHost(bundle.getString(mContext.getPackageName() + ".proxy_host"));
		if (bundle.containsKey(mContext.getPackageName() + ".proxy_port"))
			mProxyDetails.setProxyPort(bundle.getInt(mContext.getPackageName() + ".proxy_port"));
		if (bundle.containsKey(mContext.getPackageName() + ".proxy_uid"))
			mProxyDetails.setProxyUserID(bundle.getString(mContext.getPackageName() + ".proxy_uid"));
		if (bundle.containsKey(mContext.getPackageName() + ".proxy_pwd"))
			mProxyDetails.setProxyPassword(bundle.getString(mContext.getPackageName() + ".proxy_pwd"));
		
	}
	
	private void openRepository() {
		
		try {
			//mExecutor = Executors.newSingleThreadExecutor();
			//Future<SVNRepository> future = mExecutor.submit(new RunnableSVNConnect(mRepoDetails, mProxyDetails));
			SVNConnect svnConnect = new SVNConnect(mRepoDetails, mProxyDetails);
			
			svnConnect.execute();
			
			
			mRepository.testConnection();
			
			mLatestRevision = mRepository.getLatestRevision();
			//Toast.makeText(mContext, Long.toString(repository.getLatestRevision()), Toast.LENGTH_LONG).show();
		} catch (Exception e){
			Log.d(LOG, e.getMessage());
		}
	}
	
	private void closeRepository() {
		mRepository.closeSession();
		if (!mExecutor.isShutdown())
			mExecutor.shutdown();
	}
	
	public HashMap<Integer, String> getFileList() {
		HashMap<Integer, String> fileList = new HashMap<Integer, String>();
		int count = 0;
		
		openRepository();
		
		//perform actions on repository
		SVNProperties props = null;
		Collection<?> dirEntries = null;
		
		try {
			
			Collection<?> entries = mRepository.getDir(mRepoDetails.getRepositoryPath(), mLatestRevision, props, dirEntries);
			
			for (final Object dirEntry : entries.toArray()) {
				fileList.put(count, ((SVNDirEntry)dirEntry).getName());
				count++;
			}
			
		} catch (SVNException e) {
			Log.d(LOG, e.getMessage());
		}
		
		closeRepository();
		
		return fileList;
	}
}

